package nobu.nakata.nfcfare.resource.mapper;


public class ResourceMapper<Entity, History, Resource, Key> {
	private IPersistenceStrategy<Resource, Key> caching;
	private IPersistenceStrategy<Resource, Key> persistence;
	
	public ResourceMapper(IPersistenceStrategy<Resource, Key> caching, 
			IPersistenceStrategy<Resource, Key> persistence) {
		this.caching		= caching;
		this.persistence	= persistence;
	}
	
	
	public Resource findByKey(Key key) {
		Resource cached = caching.get(key);
		if (cached != null)
			return cached;
		
		cached = persistence.get(key);
		if (cached != null)
			caching.set(cached);
		return cached;
	}
	
	public void set(Resource item) {
 		caching.remove(item);
		persistence.set(item);		
	}
}
